package com.fancyy.scm.activity;

import android.os.Bundle;
import android.view.View;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.adapter.BRBaseAdapter;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.base.http.BRDataListener;
import com.fancyy.scm.base.http.BRDataTransport;
import com.fancyy.scm.bean.TrayReportItem;
import com.fancyy.scm.databinding.ActivityTrayScanReportBinding;
import com.fancyy.scm.util.DateUtil;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.fancyy.scm.BRConstant.GETTRAYLIST;


@Translucent
@Layout(R.layout.activity_tray_scan_report)
public class TrayScanReportActivity extends BaseActivity<ActivityTrayScanReportBinding> {
    BRBaseAdapter adapter = new BRBaseAdapter();
    List<Object> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter.init(this, R.layout.trayreportitem, list);
        getDataBinding().listView.setAdapter(adapter);
        getDataBinding().begin.setText(DateUtil.getToday());
        getDataBinding().end.setText(DateUtil.getToday());
    }


    public void onTrayReportSearchClick(View view) {
        String url = GETTRAYLIST;
        BRDataTransport.create(url)
                .addParam("UserJobNum","")
                .addParam("StartDate", getDataBinding().begin.getText().toString())
                .addParam("EndDate", getDataBinding().end.getText().toString())
                .addParam("TrayBarCode", "")
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener<List<TrayReportItem>>() {
                    @Override
                    public void onSuccess(List<TrayReportItem> data) {
                        list.clear();
                        if (data != null && data.size() > 0) {
                            list.addAll(data);
                        } else {
                            showToast("No data be found");
                        }
                        adapter.notifyDataSetChanged();
                    }
                }, new TypeToken<List<TrayReportItem>>(){});
    }


    public void onEndDateClick(View view) {
        Calendar calendar = Calendar.getInstance();
        Date date = DateUtil.toDate(getDataBinding().begin.getText().toString());
        calendar.setTime(date);
        DateUtil.showDatePickerDialog(this, getDataBinding().end, calendar);
    }

    public void onBeginDateClick(View view) {
        Calendar calendar = Calendar.getInstance();
        Date date = DateUtil.toDate(getDataBinding().begin.getText().toString());
        calendar.setTime(date);
        DateUtil.showDatePickerDialog(this, getDataBinding().begin, calendar);
    }
}
